package com.xsoft.demo10;

import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class GoodsDetailActivity extends AppCompatActivity {
    private TextView title;
    private ImageView pictureAlbum;
    private TextView name;
    private ImageView picture;
    private TextView price;
    private TextView detail;
    private TextView tabDetail;
    private TextView tabEvaluate;
    private TextView tabRecommend;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.shop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ShopDataBaseHelper(GoodsDetailActivity.this).insert((GoodsInfo) getIntent().getParcelableExtra("data"));
                Toast.makeText(GoodsDetailActivity.this, "已添加到购物车", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(v.getContext()).inflate(R.layout.view_menu, null);
                PopupWindow popupWindow = new PopupWindow(view,
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popupWindow.setTouchable(true);
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return false;
                    }
                });
                popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
                popupWindow.showAsDropDown(v, 0, 0);
            }
        });
        title = findViewById(R.id.title);
        tabDetail = (TextView) findViewById(R.id.tab_detail);
        tabEvaluate = (TextView) findViewById(R.id.tab_evaluate);
        tabRecommend = (TextView) findViewById(R.id.tab_recommend);
        tabDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabDetail.setSelected(true);
                tabEvaluate.setSelected(false);
                tabRecommend.setSelected(false);
            }
        });
        tabEvaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabDetail.setSelected(false);
                tabEvaluate.setSelected(true);
                tabRecommend.setSelected(false);
            }
        });
        tabRecommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tabDetail.setSelected(false);
                tabEvaluate.setSelected(false);
                tabRecommend.setSelected(true);
            }
        });
        pictureAlbum = (ImageView) findViewById(R.id.picture_album);
        name = (TextView) findViewById(R.id.name);
        picture = (ImageView) findViewById(R.id.picture);
        price = (TextView) findViewById(R.id.price);
        detail = (TextView) findViewById(R.id.detail);
        GoodsInfo info = getIntent().getParcelableExtra("data");
        title.setText(info.getName());
        name.setText(info.getName());
        price.setText("¥" + info.getPrice());
        detail.setText(info.getDetail());
        try {
            pictureAlbum.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(info.getPictureAlbum())));
            picture.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(info.getPicture())));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
